package com.iterator.service.web.promotionService;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iterator.bean.web.pojo.zql.InfoData;
import com.iterator.bean.web.pojo.zql.Topic;
import com.iterator.bean.web.pojo.zql.TopicExample;
import com.iterator.bean.web.zql.bo.TopicBO;
import com.iterator.mapper.web.promotionMapper.TopicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author : L
 * @ProjectName : project-2
 * @Date : 1/9/2021
 * @Description :
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;

    @Override
    public InfoData<Topic> queryTopicList(TopicBO topicBO) {
        PageHelper.startPage(topicBO.getPage(), topicBO.getLimit());
        TopicExample topicExample = new TopicExample();
        topicExample.setOrderByClause(topicBO.getSort() + " " + topicBO.getOrder());
        TopicExample.Criteria criteria = topicExample.createCriteria();
        if (topicBO.getTitle() != null) {
            criteria.andTitleLike("%"+topicBO.getTitle()+"%");
        }
        if (topicBO.getSubtitle() != null) {
            criteria.andSubtitleLike("%"+topicBO.getSubtitle()+"%");
        }
        InfoData<Topic> topicInfoData = new InfoData<>();
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        topicInfoData.setItems(topics);

        PageInfo<Topic> topicPageInfo = new PageInfo<>(topics);
        long total = topicPageInfo.getTotal();
        topicInfoData.setTotal((int) total);

        return topicInfoData;
    }

    @Override
    public Topic insertTopic(Topic topic) {
        Date addTime = new Date();
        topic.setAddTime(addTime);
        topicMapper.insertSelective(topic);
        return topic;
    }

    @Override
    public Topic updateTopic(Topic topic) {
        Date updateTime = new Date();
        topic.setUpdateTime(updateTime);
        topicMapper.updateByPrimaryKeySelective(topic);
        return topic;
    }

    @Override
    public Integer deleteTopic(Topic topic) {
        int i = topicMapper.deleteByPrimaryKey(topic.getId());
        return i;
    }
}
